<?php
class Admin_RolesController extends Zend_Controller_Action
{
    protected $_roles;
    protected $_departments;
    protected $_categories;
    protected $_acl;

    public function init() {
        $this->_helper->layout()->setLayout('admin');

        $this->_roles       = new Admin_Model_Roles();
        $this->_departments = new Admin_Model_Department();
        $this->_categories  = new Admin_Model_Category();       
        $this->_acl         = new Application_Model_Acl();
    }

    public function indexAction() {
        $this->view->allRoles = $this->_roles->getAllRoles();       
    }

    public function addRoleAction() {       
        $this->view->departments   = $this->_departments->getAllDepartments();
        $this->view->resources     = $this->_acl->getAllResources();    
        $this->view->categoryTypes = $this->_categories->getAllCategoryTypes(); 
    }

    public function editRoleAction() {
        $roleId = $this->_getParam('role');
        
        $roleDepartments = $this->_roles->getRoleDepartments($roleId);      
        $roleDepartmentsIds = '';
        $isFirst = true;
        foreach ($roleDepartments as $roleDepartment) {
            $comma = !$isFirst?',':'';
            $roleDepartmentsIds .= $comma . $roleDepartment['id'];
            $isFirst = false;
        }       
        $this->view->departments = $this->_departments->getAllDepartmentsExcept(
            $roleDepartmentsIds
        );

        $roleResources = $this->_acl->getAllRoleResources($roleId);
        $roleResourcesIds = '';
        $isFirst = true;
        foreach ($roleResources as $roleResource) {
            $comma = !$isFirst?',':'';
            $roleResourcesIds .= $comma . $roleResource['id'];
            $isFirst = false;
        }
        $this->view->resources = $this->_acl->getAllResourcesExcept(
            $roleResourcesIds
        );      


        $roleCategoryTypes = $this->_roles->getRoleCategoryTypes($roleId);
        $roleCategoryTypesIds = '';
        $isFirst = true;
        foreach ($roleCategoryTypes as $roleCategoryType) {
            $comma = !$isFirst?',':'';
            $roleCategoryTypesIds .= $comma . $roleCategoryType['id'];
            $isFirst = false;
        }       
        $this->view->categoryTypes = $this->_categories->getAllCategoryTypesExcept(
            $roleCategoryTypesIds
        );

        $this->view->currentRole       = $this->_roles->getRoleById($roleId);
        $this->view->roleDepartments   = $roleDepartments;
        $this->view->roleResources     = $roleResources;
        $this->view->roleCategoryTypes = $roleCategoryTypes;
        $this->view->allDepartments    = $this->_departments->getAllDepartments();
        $this->view->roleCategories    = $this->_categories->getAvailableForRoleCategories($roleId);
    }

    public function deleteRoleAction() {
        $roleId = $this->_getParam('role');

        $this->_roles->deleteRole($roleId);
        $this->_roles->deleteRoleCategories($roleId);
        $this->_roles->deleteRoleCategoryTypes($roleId);
        $this->_roles->deleteRoleDepartments($roleId);
        $this->_roles->deleteRoleResources($roleId);

        My_Cache::clearCache('acl');

        $this->_helper->redirector('index', 'roles', 'admin');
    }
}